var app = getApp();
Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0, // tab切换 
    isHiddenMes1: true,
    isHiddenMes2: true,
    isHiddenMes4: true,

  
    weiRun: [],//未进行
    mine_pageIndex: 1,//分页数据
    mine_allpages: 0,//总页数
    mineTravel_list: [
      {}
    ],
    pic_array:['从收藏的行程中创建','自助创建'],

    collec_pageIndex:1,//分页数据
    collec_allpages:0,//总页数
    collecAry: [
      {  
      }
    ],
  },
  // 一个页面只会调用一次。
  onLoad: function (options) {
    var that = this;
    // app.loading();
    //获取设备宽高
    that.setData({
      winWidth: app.globalData.systemInfo.windowWidth,
      winHeight: app.globalData.systemInfo.windowHeight
    }); 
    
    if (this.data.mineTravel_list.length > 0) {
      that.setData({
        isHiddenMes4: false
      })
    }
    if (this.data.collecAry.length > 0) {
      that.setData({
        isHiddenMes1: false
      })
    }

    this.get_recommend_trip();//推荐行程
    this.get_mine_trip();//我的行程

  },
  // 每次打开页面都会调用一次。
  onShow: function () {
    var currentTab = this.data.currentTab;

    // if (currentTab==0){
    //   //post获取-推荐行程-数据
      
    //   this.get_recommend_trip();

    // } else if (currentTab ==2){
    //   // post获取-我的行程-数据
    //   this.get_mine_trip();
    // }

  },
  /**
   * 我的行程-数据
   */
  get_mine_trip:function(){
    var that = this;
    app.apiPost(app.apiList.userTravel, {
      content: {
        pageNum: that.data.mine_pageIndex,
        pageSize: 10
      }
    }, (res) => {
      console.log(res,'我的行程');
      var list = res.content.list;
      /*
      mine_pageIndex: 1,//分页数据
    mine_allpages: 0,//总页数
      */ 
      if (that.data.mine_pageIndex == 1) {
        that.setData({
          mineTravel_list: list,
          mine_allpages: res.content.total
        })
      } else {
        var ary = that.data.mineTravel_list;
        ary = ary.concat(list);
        that.setData({
          mineTravel_list: ary,
          mine_allpages: res.content.total
        })
      }

    })
  },
  /**
   * 推荐行程-数据
   */
  get_recommend_trip:function(){
    var that = this;
    app.apiPost(app.apiList.recommend_trip, {
      content: {
        pageNum: that.data.collec_pageIndex,
        pageSize: 10
      }
    }, (res) => {
      console.log(res.content.list, "收藏行程",res);
      var list = res.content.list;

      if (that.data.collec_pageIndex == 1){
        that.setData({
          collecAry: list,
          collec_allpages: res.content.total
        })
      }else{
        var ary = that.data.collecAry;
        ary = ary.concat(list);
        that.setData({
          collecAry: ary,
          collec_allpages: res.content.total
        })
      }
    })
  },
  /**
   * 下拉刷新
   */
  refresh:function(e){
    console.log(e.currentTarget.dataset.cla,'888');
    var self = this;
    if (e.currentTarget.dataset.cla =="recommen"){
      setTimeout(function () {
        console.log('下拉刷新');
        var date = new Date();
        self.setData({
          collec_pageIndex: 1
        })
        self.get_recommend_trip();
      }, 300);
    } else if (e.currentTarget.dataset.cla == "mine"){
      setTimeout(function () {
        
        var date = new Date();
        self.setData({
          mine_pageIndex: 1
        })
        self.get_mine_trip();
      }, 300);
    }
    
  },
  /**
   *加载更多
   */
  loadMore:function(e){
    var self = this;
    // 当前页是最后一页
    if (self.data.collec_pageIndex == self.data.collec_allpages) {
      self.setData({
        loadMoreData: '已经到顶'
      })
      return;
    }
    if (e.currentTarget.dataset.cla == "recommen"){
      setTimeout(function () {
        console.log('上拉加载更多');
        var tempCurrentPage = self.data.collec_pageIndex;
        tempCurrentPage = tempCurrentPage + 1;
        self.setData({
          collec_pageIndex: tempCurrentPage,
          hideBottom: false
        })
        self.get_recommend_trip();
      }, 300);
    } else if (e.currentTarget.dataset.cla == "mine") {
      setTimeout(function () {
        console.log('上拉加载更多');
        var tempCurrentPage = self.data.mine_pageIndex;
        tempCurrentPage = tempCurrentPage + 1;
        self.setData({
          mine_pageIndex: tempCurrentPage,
          hideBottom: false
        })
        self.get_mine_trip();
      }, 300);
    }
    
  },
  // 一个页面只会调用一次
  onReady: function () {

  },
  //当navigateTo或底部tab切换时调用。
  onHide: function () {

  },
  //当redirectTo或navigateBack的时候调用。
  onUnload: function () {

  },
  /**
   * 我的行程-详情
   */
  orderDetail:function(e){

    console.log(e.currentTarget.dataset.id, '我的行程 - 详情');

    var obj = {
      id: e.currentTarget.dataset.id,
      from_name: 'userTravel',
      page_type: 1
    }
    obj = JSON.stringify(obj);

    wx.navigateTo({
      url: '../collecrecomend/collecrecomend?obj=' + obj,
    })
  },
  /**
   * 推荐行程操作
   */
  clickCollec_item: function (data) {
    console.log(data.detail, '子组件传值');
    var obj = {
      id: data.detail,
      from_name:'recommend',
      page_type:1
    }
    console.log('推荐行程id', obj.id);
    /* 1:行程计划  2：自助行程-从收藏的行程中创建*/
    obj = JSON.stringify(obj);
    wx.navigateTo({
      url: '../collecrecomend/collecrecomend?obj=' + obj
    })
  },
  //点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /**
   * 滑动切换tab
   */
  bindChangeTab: function (e) {

    this.setData({ currentTab: e.detail.current });

  },
  /**
   * 自主行程-选择器
   */
  bindPickerChange:function(e){
   console.log(e.detail.value,'选择器');

    var index = parseInt(e.detail.value);
    if(index==0){
      wx.navigateTo({
        url: '../collectiontravel/collectiontravel',
      })
    } else if (index == 1){
    //  自助行程
      wx.navigateTo({
        url: '../creattravel/creattravel',
      })
    }
  },

  // orderDetail: function (e) {
  //   console.log('查看详情', e.currentTarget.dataset.index, e.currentTarget.dataset.id);
  // },
  // deleteOrder: function (e) {
  //   console.log('删除订单--通过type找到对应的数组', e.currentTarget.dataset.type, e.currentTarget.dataset.index);

  // },
  // buyAgain: function (e) {
  //   console.log('再次购买--通过索引值获取对应数组的对象', e.currentTarget.dataset.index);

  // }

})

// https://blog.csdn.net/shaleilei/article/details/78345508 模板传值
// https://blog.csdn.net/github_39371177/article/details/80251211 多级菜单